from time import sleep

import pytest
import requests


from api.api_register_login import ApiRegisterLogin
from tools import common_assert, read_json, log, clear_data


class Test01RegisterLogin:
    def setup_class(self):
        clear_data()

    def setup(self):
        self.session=requests.session()
        log.info("正在获取session对象：{}".format(self.session))
        self.api=ApiRegisterLogin(self.session)
        log.info("正在获取ApiRegisterLogin对象：{}".format(self.api))
    def teardown(self):
        self.session.close()
        log.info("正在关闭session对象：{}".format(self.session))

    @pytest.mark.parametrize("random,expect_code",read_json("register_login.json","img_code"))
    def test01_img_code(self,random,expect_code):
        try:
            result=self.api.api_img_code(random)
            log.info("图片验证码接口测试结果为：{}".format(result.status_code))
            common_assert(result,status_code=expect_code)
            log.info("图片验证码接口断言通过！")
        except Exception as e:
            log.error(e)
            raise
    @pytest.mark.parametrize("phone,imgVerifyCode,expect_json",read_json("register_login.json","phone_code"))
    def test02_phone_code(self,phone,imgVerifyCode,expect_json):
        try:
            self.api.api_img_code(123456)
            result=self.api.api_phone_code(phone,imgVerifyCode)
            log.info("短信验证码接口测试结果为：{}".format(result.text))
            common_assert(result,expect_json=expect_json)
            log.info("短信验证码接口断言通过！")
        except Exception as e:
            log.error(e)
            raise
    @pytest.mark.parametrize("phone,password,verifycode,phone_code,expect_json",read_json("register_login.json","register"))
    def test03_register(self,phone,password,verifycode,phone_code,expect_json):
        try:
            self.api.api_img_code(123456)
            self.api.api_phone_code(phone, "8888")
            result =self.api.api_register(phone,password,verifycode,phone_code)
            common_assert(result,expect_json=expect_json)
        except Exception as e:
            log.error(e)
            raise

    @pytest.mark.parametrize("phone,password,expect_json",
                             read_json("register_login.json", "login"))
    def test04_login(self,phone,password,expect_json):
        try:
            if "error" in password:
                result=None
                i=1
                while i <=3:
                    result=self.api.api_login(phone, password)
                    log.info("登录接口测试结果为：{}".format(result.text))
                    i+=1
                assert "锁定" in result.text
                sleep(60)
                result = self.api.api_login(phone, "test123")
                common_assert(result, expect_json=expect_json)
            else:
                result =self.api.api_login(phone,password)
                log.info("登录测试结果为：{}".format(result.text))
                common_assert(result, expect_json=expect_json)
            log.info("登录断言通过！")
        except Exception as e:
            log.error(e)
            raise

    @pytest.mark.parametrize("status,expect_json",
                             read_json("register_login.json", "login_status"))
    def test05_login_status(self,status,expect_json):
        try:
            if status=="已登录":
                self.api.api_login("15937079911", "test123")
            result=self.api.api_login_status()
            log.info("查询登录状态测试结果为：{}".format(result.text))
            common_assert(result,expect_json=expect_json)
            log.info("查询登录状态 断言通过！")
        except Exception as e:
            log.error(e)
            raise
